﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace CIT.WMS.houseWork.Bll
{
    public class Bll_Inventory_ti
    {

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="varWhere"></param>
        /// <returns></returns>
        public static DataTable Query(string varWhere)
        {
            string sqlcmd = string.Format("select *  from T_Inventory_ti {0}", varWhere);
            return Utils.NMS.QueryDataTable(Utils.uContext, sqlcmd, new CIT.WMS.MESWcf.CmdParameter[0]);
        }
        /// <summary>
        /// 校验盘点单号
        /// </summary>
        /// <param name="varInventory">盘点单号</param>
        /// <param name="varMsg">返回结果</param>
        /// <returns></returns>
        public static bool CheckInventoryCode(string varInventoryCode, out string varMsg)
        {
            string sqlcmd = string.Format(@"
if not exists(select * from T_Inventory_ti where InventoryCode='{0}')
    begin
        select '0','盘点单号错误' return
    end
else
    begin
        --校验盘点单是否已经完成,已经完成则停止操作
		if not exists(select *  from dbo.T_Inventory_ti where InventoryCode='{0}' and Status<>'2')
			begin
				select '0','盘点单已汇总，请重新选择' return
			end
        else
            begin
                 select '1','Success' return
            end     
    end", varInventoryCode);
            DataTable dt_check = Utils.NMS.QueryDataTable(Utils.uContext, sqlcmd, new CIT.WMS.MESWcf.CmdParameter[0]);
            if (dt_check.Rows[0][0].ToString() == "0")
            {
                varMsg = Helper.SqlInput.ChangeNullToString(dt_check.Rows[0][1]);
                return false;
            }
            else
            {
                varMsg = Helper.SqlInput.ChangeNullToString(dt_check.Rows[0][1]);
                return true;
            }
        }

        /// <summary>
        /// 更新料盘盘点数量（三个步骤）
        /// </summary>
        /// <param name="InvenToryQty">盘点的数量</param>
        /// <param name="varTotalQty">料盘原有的总数量(差异数=料盘原有的总数量-盘点的数量)</param>
        /// <param name="varInventoryCode">盘点单据号</param>
        /// <param name="varReelid">Reelid</param>
        /// <param name="varPN">料号</param>
        /// <returns></returns>
        public static bool UpdateBarCodeQty(string InvenToryQty, string varTotalQty, string varInventoryCode, string varReelid, string varPN)
        {
            string sqlcmd = string.Format(@"
declare @CurrentQty nVarchar(50)
 --1查看该单据号下Reelid是否被盘过
select @CurrentQty=CurrentQty  from  dbo.T_InventoryDetail_tid where Status='0' and InventoryCode='{2}' and ReelId='{3}'
if(@CurrentQty<>'' and @CurrentQty<>'0')
	begin
	   --1.1如果已被盘过，先减掉已盘数量
	  update T_Inventory_Detail set CurrentQty=convert(decimal,Convert(decimal,CurrentQty)-convert(decimal,@CurrentQty)) where InventoryCode='{2}' and PN='{4}'	
	end
--2.更新料盘 T_InventoryDetail_tid（盘点管理料盘详细表）
update T_InventoryDetail_tid set CurrentQty=convert(decimal,'{0}'),DifferQty=convert(decimal,convert(decimal,'{0}')-convert(decimal,'{1}')),Status='0' where InventoryCode='{2}' and Reelid='{3}' 
--3.更新 T_Inventory_Detail（盘点单 盘点单详细从表）
--3.1加上现盘数量
update T_Inventory_Detail set CurrentQty=convert(decimal,Convert(decimal,CurrentQty)+convert(decimal,'{0}')) where InventoryCode='{2}' and PN='{4}'
--3.2更新 T_Inventory_ti（盘点单主表状态 盘点中）
update T_Inventory_ti set Status='1' where InventoryCode='{2}'  ",
InvenToryQty, varTotalQty, varInventoryCode, varReelid, varPN);
            bool result, varflag;
            Utils.NMS.Exectransql(Utils.uContext, sqlcmd, new CIT.WMS.MESWcf.CmdParameter[0], out result, out varflag);
            return result;
        }
    }
}
